package org.lld.im.service.utils;

import org.lld.im.common.ResponseVO;
import org.lld.im.common.config.AppConfig;
import org.lld.im.common.utils.HttpRequestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


/**
 * @author: LunBoWang
 * @description: 回调服务
 * @date: 2025/3/31 下午6:29
 * @version: 1.0
 */
@Component
public class CallbackService {
    private Logger logger= LoggerFactory.getLogger(CallbackService.class);

    @Resource
    HttpRequestUtils httpRequestUtils;

    @Resource
    AppConfig appConfig;

    @Resource
    ShareThreadPool shareThreadPool;

    public void callback(Integer appId,String callbackCommand,String jsonBody){
        shareThreadPool.submit(()->{
            try {
                httpRequestUtils.doPost(appConfig.getCallbackUrl(),Object.class,buliderUrlParams(appId,callbackCommand),jsonBody,null);
            } catch (Exception e) {
                logger.error("callback 回调{}:{}出现异常：{}",callbackCommand,appId,e.getMessage());
            }
        });
    }

    public ResponseVO beforeCallback(Integer appId,String callbackCommand,String jsonBody){
        try {
            ResponseVO responseVO = httpRequestUtils.doPost(appConfig.getCallbackUrl(), ResponseVO.class, buliderUrlParams(appId, callbackCommand), jsonBody, null);
            return responseVO;
        } catch (Exception e) {
            logger.error("callback 之前回调{}:{}出现异常：{}",callbackCommand,appId,jsonBody,e);
            return ResponseVO.successResponse();
        }
    }

    public Map buliderUrlParams(Integer appId,String command){
        Map map=new HashMap();
        map.put("appId",appId);
        map.put("command",command);
        return map;
    }
}
